package com.smartTerminal.utils;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.smartTerminal.perMatters.entity.ItemInfo;
import com.smartTerminal.user.entity.User;

/**
 * 封装xml字符串
 * @author wantao
 * @Date 2018年1月10日
 *
 */
public class XmlUtils {

	/**
	 * 生成xml字符串
	 * @param username 预约人姓名
	 * @param userphone 预约人电话
	 * @param usercard 预约人身份证号码
	 * @param usertime 预约时间，严格使用getItemAppointmentTime接口返回的yysj
	 * @param useritemId 预约事项id
	 * @param userdeptId 部门id
	 * @return xml字符串
	 */
	public static String createXml(String username,String userphone,String usercard,String usertime,String useritemId,String userdeptId) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.newDocument();
		document.setXmlStandalone(true);

		Element auditRegInfo = document.createElement("auditRegInfo");
		document.appendChild(auditRegInfo);

		Element name = document.createElement("name");
		name.setTextContent(username);
		auditRegInfo.appendChild(name);
			
		Element phone = document.createElement("phone");
		phone.setTextContent(userphone);
		auditRegInfo.appendChild(phone);
			
		Element idcard = document.createElement("idcard");
		idcard.setTextContent(usercard);
		auditRegInfo.appendChild(idcard);
			
		Element time = document.createElement("time");
		time.setTextContent(usertime);
		auditRegInfo.appendChild(time);
			
		Element itemId = document.createElement("itemId");
		itemId.setTextContent(useritemId);
		auditRegInfo.appendChild(itemId);
			
		Element siteid = document.createElement("siteid");
		siteid.setTextContent(userdeptId);
		auditRegInfo.appendChild(siteid);

		TransformerFactory transFactory = TransformerFactory.newInstance();
		Transformer transformer = transFactory.newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		DOMSource domSource = new DOMSource(document);

		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		transformer.transform(domSource, new StreamResult(bos));
		String xmlInfo = bos.toString("utf-8");	
		return xmlInfo;
	}
	
	public  static String createXml(ItemInfo itemInfo) throws Exception{
		//获取用户身份(区分个人和企业)
		String userType = itemInfo.getUserType();
				
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.newDocument();
		document.setXmlStandalone(true);
				
		Element auditRegInfo = document.createElement("auditRegInfo");
		document.appendChild(auditRegInfo);
			
		Element userid = document.createElement("userid");
		userid.setTextContent(itemInfo.getUserid());
		auditRegInfo.appendChild(userid);
			
		Element itemId = document.createElement("itemId");
		itemId.setTextContent(itemInfo.getItemId());
		auditRegInfo.appendChild(itemId);
		
		Element itemsxbm = document.createElement("itemsxbm");
		itemsxbm.setTextContent(itemInfo.getItemsxbm());
		auditRegInfo.appendChild(itemsxbm);
				
		Element register = document.createElement("register");
		register.setTextContent(itemInfo.getRegister());
		auditRegInfo.appendChild(register);
				
		Element enterpriseName = document.createElement("enterpriseName");
		enterpriseName.setTextContent(itemInfo.getEnterpriseName());
		auditRegInfo.appendChild(enterpriseName);
		
		if("0".equals(userType)){
			Element idcard = document.createElement("idcard");
			idcard.setTextContent(itemInfo.getIdcard());
			auditRegInfo.appendChild(idcard);
			
			Element name = document.createElement("name");
			name.setTextContent(itemInfo.getName());
			auditRegInfo.appendChild(name);
			
			Element phone = document.createElement("phone");
			phone.setTextContent(itemInfo.getPhone());
			auditRegInfo.appendChild(phone);
			
			Element agentId = document.createElement("agentId");
			agentId.setTextContent(itemInfo.getAgentId());
			auditRegInfo.appendChild(agentId);
			
		}else if("1".equals(userType)){
			Element enterprisePerson = document.createElement("enterprisePerson");
			enterprisePerson.setTextContent(itemInfo.getEnterprisePerson());
			auditRegInfo.appendChild(enterprisePerson);
			
			Element businesslicence = document.createElement("businesslicence");
			businesslicence.setTextContent(itemInfo.getBusinesslicence());
			auditRegInfo.appendChild(businesslicence);
			
			Element name = document.createElement("name");
			name.setTextContent(itemInfo.getName());
			auditRegInfo.appendChild(name);
			
			Element phone = document.createElement("phone");
			phone.setTextContent(itemInfo.getPhone());
			auditRegInfo.appendChild(phone);
			
			Element idcard = document.createElement("idcard");
			idcard.setTextContent(itemInfo.getIdcard());
			auditRegInfo.appendChild(idcard);
			
			Element address = document.createElement("address");
			address.setTextContent(itemInfo.getAddress());
			auditRegInfo.appendChild(address);
		}
				
		Element isExprecss = document.createElement("isExprecss");
		isExprecss.setTextContent(itemInfo.getIsExprecss());
		auditRegInfo.appendChild(isExprecss);
			
		Element exprecssId = document.createElement("exprecssId");
		exprecssId.setTextContent(itemInfo.getExprecssId());
		auditRegInfo.appendChild(exprecssId);
		
		TransformerFactory transFactory = TransformerFactory.newInstance();
		Transformer transformer = transFactory.newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		DOMSource domSource = new DOMSource(document);
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		transformer.transform(domSource, new StreamResult(bos));
		String xmlInfo = bos.toString("utf-8");
		
		return xmlInfo;
	}
	
	/**
	 * 封装用户注册xml
	 * @method	createXml
	 * @author	Xia.Zhuming
	 * @data	2018年1月13日
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public static String createXml(User user) throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.newDocument();
		document.setXmlStandalone(true);
				
		Element auditRegInfo = document.createElement("auditRegInfo");
		document.appendChild(auditRegInfo);
			
		Element userType = document.createElement("userduty");
		userType.setTextContent(user.getUserType());
		auditRegInfo.appendChild(userType);
			
		Element realName = document.createElement("realname");
		realName.setTextContent(user.getRealName());
		auditRegInfo.appendChild(realName);
				
		Element phone = document.createElement("phone");
		phone.setTextContent(user.getPhone());
		auditRegInfo.appendChild(phone);
						
		Element idCard = document.createElement("idcard");
		idCard.setTextContent(user.getIdCard());
		auditRegInfo.appendChild(idCard);
						
		Element sex = document.createElement("sex");
		sex.setTextContent(user.getSex());
		auditRegInfo.appendChild(sex);
				
						
		Element ethnic = document.createElement("ethnic");
		ethnic.setTextContent(user.getEthnic());
		auditRegInfo.appendChild(ethnic);
					
		Element address = document.createElement("address");
		address.setTextContent(user.getAddress());
		auditRegInfo.appendChild(address);
				
		TransformerFactory transFactory = TransformerFactory.newInstance();
		Transformer transformer = transFactory.newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		DOMSource domSource = new DOMSource(document);
				
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		transformer.transform(domSource, new StreamResult(bos));
		String xmlInfo = bos.toString("UTF-8");
			
		return xmlInfo;
	}
	
	
	/**
	 * 
	 * @param user
	 * @return  
	 * @throws Exception 
	 * @throws Exception 
	 * @throws Exception
	 */
	public static String createXml(ItemInfo itemInfo,String type) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.newDocument();
		document.setXmlStandalone(true);
				
		Element auditRegInfo = document.createElement("auditRegInfo");
		document.appendChild(auditRegInfo);
			
		Element register = document.createElement("register");
		register.setTextContent(itemInfo.getRegister());
		auditRegInfo.appendChild(register);
		
		Element name = document.createElement("name");
		name.setTextContent(itemInfo.getName());
		auditRegInfo.appendChild(name);
			
		Element phone = document.createElement("phone");
		phone.setTextContent(itemInfo.getPhone());
		auditRegInfo.appendChild(phone);
				
		Element postcode = document.createElement("postcode");
		postcode.setTextContent(itemInfo.getYzbm());
		auditRegInfo.appendChild(postcode);
						
		Element time = document.createElement("time");
		time.setTextContent(itemInfo.getTime());
		auditRegInfo.appendChild(time);
						
		Element prov = document.createElement("prov");
		prov.setTextContent(itemInfo.getCmbProvince());
		auditRegInfo.appendChild(prov);
				
						
		Element city = document.createElement("city");
		city.setTextContent(itemInfo.getCmbCity());
		auditRegInfo.appendChild(city);
					
		Element address = document.createElement("address");
		address.setTextContent(itemInfo.getCmbArea()+itemInfo.getAddress());
		auditRegInfo.appendChild(address);
				
		Element prokeyid = document.createElement("prokeyid");
		prokeyid.setTextContent(itemInfo.getProkeyid());
		auditRegInfo.appendChild(prokeyid);
		
		TransformerFactory transFactory = TransformerFactory.newInstance();
		Transformer transformer = transFactory.newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		DOMSource domSource = new DOMSource(document);
				
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		transformer.transform(domSource, new StreamResult(bos));
		String xmlInfo = bos.toString("UTF-8");
			
		return xmlInfo;
	}
}
